//
//  WBStatusViewModel.swift
//  微博
//
//  Created by 康智桐 on 2018/5/1.
//  Copyright © 2018年 Kk. All rights reserved.
//

import Foundation


 
class WBStatusViewModel :CustomStringConvertible{
    
    //微博模型
    var status:WBStatus
    
    //微博会员图标
    var memberIcon:UIImage?
    
    //认证图标
    var vipIcon:UIImage?
    
    
    
    
    
    //转发文字
    var retweetedStr:String?
    
    //评论文字
    var commentStr:String?
    
    //点赞文字
    var  likeStr:String?
    
    //微博信息来源
    var sourceStr:String?
    
    
    //配图视图大小
    var pictrueViewSize = CGSize()
    
    //如果是被转发的微博，原创微博一定没有配图
    var picURLs:[WBStatusPicture]? {
        
        return status.retweeted_status?.pic_urls ?? status.pic_urls
        
    }
    
    //微博正文的属性文本
    var statusAttrText:NSAttributedString?

    
    //被转发微博的文字
    var retweetedAttrText:NSAttributedString?
    
    
    //行高
    var rowHeight:CGFloat = 0
    
    
    
    
    
    init(model:WBStatus) {
        self.status = model
        
        //会员等级0-6
        if (model.user?.mbrank)! > 0 && (model.user?.mbrank)! < 7 {
            let  imageName = "common_icon_membership_level\(model.user?.mbrank ?? 1)"
            
            memberIcon = UIImage(named: imageName)
        }
        switch model.user?.verified_type ?? -1 {
        case 0:
            vipIcon = UIImage(named: "avatar_vip")
        case 2,3,5:
            vipIcon = UIImage(named: "avatar_enterprise_vip")
        case 220 :
            vipIcon = UIImage(named: "avatar_grassroot")
        default:
            break
        }
        
        //设置底部计数字符串
        
        retweetedStr = countString(count: model.reposts_count, defaultStr: "转发")
        commentStr = countString(count: model.comments_count, defaultStr: "评论")
        likeStr = countString(count: model.attitudes_count, defaultStr: "赞")
        
        
        //计算配图视图大小
        pictrueViewSize =  calcPictureViewSize(count: picURLs?.count)
        
        
        //设置微博文本
        let originalFont = UIFont.systemFont(ofSize: 15)
        let retweetedFont = UIFont.systemFont(ofSize: 14)

        //微博正文的属性文本
        statusAttrText = CZEmoticonManager.shared.emoticonString(string: model.text ?? "", font: originalFont)
        
        
        //设置被转发微博的文字
       let rText = "@" + (status.retweeted_status?.user?.screen_name ?? "" ) + ":" + (status.retweeted_status?.text ?? "")
        
        retweetedAttrText = CZEmoticonManager.shared.emoticonString(string: rText, font: retweetedFont)
        
        //设置来源字符
        sourceStr = "来自于 " + (model.source?.cz_href()?.text ?? "")
        
        
        //计算行高
        updateRowHeight()
        
        
    }
    
    var description: String{
        return status.description
    }
    
    
    
    //根据当前的视图模型内容计算行高
    func updateRowHeight(){
        
        let margin:CGFloat = 12
        let iconHeight:CGFloat = 34
        let toolbarHeight:CGFloat = 35
        
        var height :CGFloat = 0
        
        let viewSize = CGSize(width: UIScreen.cz_screenWidth() - 2 * margin, height: CGFloat(MAXFLOAT))
        
        
        //计算顶部位置
        height = 2 * margin + iconHeight         
        
        //正文高度
        
        if let text = statusAttrText {
            height += text.boundingRect(with: viewSize, options: [.usesLineFragmentOrigin], context: nil).height
            
        }
        
        //判断是否转发微博
        if status.retweeted_status != nil {
            
            height += 2 * margin
            
            if let text = retweetedAttrText {
                
                height += text.boundingRect(with: viewSize, options: [.usesLineFragmentOrigin], context: nil).height
            
            }
            
        }
        //配图视图
        height += pictrueViewSize.height
        
        height += margin
        
        //底部工具栏
        height += toolbarHeight
        
        //使用属性记录
        rowHeight = height
        
        
    }
    
    
    
    //使用单个图像更新配图视图的大小
    
    //支出长微博
    func updataSingleImageSize(image:UIImage){
        
        var size = image.size
        
        
        //过宽图像处理
        let maxWidth:CGFloat = 300
        let minWidth:CGFloat = 40
        
        if size.width > maxWidth {
            
            size.width = maxWidth
            
            //等比例调整
            size.height = size.width * image.size.height / image.size.width
            
        }
        
        //过窄图像处理
        if size.width < minWidth {
            size.width = minWidth
            
            //等比例调整
            size.height = size.width * image.size.height / image.size.width / 4
        }
        
        
        
        //顶部增加12个像素点
        size.height += WBStatusPictureViewOutterMargin
        
        
        //重新设置配图视图大小
        pictrueViewSize = size
        
        //更新行高
        updateRowHeight()
        
    }
    
    
    
    
    //计算指定数量图片对应的配图视图的大小
    func calcPictureViewSize(count:Int?) -> CGSize {
        
        if count == 0 || count == nil {
            return CGSize()
        }

        //计算配图视图的宽度 
       
        
        //计算高度
        //计算行数
        let row = (count! - 1 ) / 3 + 1
        
        //根据行数算高度
        var height = WBStatusPictureViewOutterMargin
            height += CGFloat(row) * WBStatusPictureItemWidth
            height += CGFloat(row - 1 ) * WBStatusPictureViewInnerMargin
        
        
        
        
        return CGSize(width: WBStatusPictureViewWidth, height: height)
    }
    
    
    
    ///给定一个数字，返回对应的描述结果
    func countString (count:Int, defaultStr:String) -> String {
        
        if count == 0 {
            return defaultStr
        }
        
        if count < 10000 {
            return count.description
        }
        
        return String(format: "%.02f 万", Double(count) / 10000)
        
    }
    
    
    
    
    
    
    
    
}
